package com.xiaoyu.lib_av.datamodel

import `in`.srain.cube.request.JsonData

class CallMessage {

    val vendorUid: String
    val senderUid: String

    private var type: String? = null
    var attrs: JsonData? = null
        private set

    private constructor(vendorUid: String, senderUid: String) {
        this.vendorUid = vendorUid
        this.senderUid = senderUid
    }

    private constructor(jsonData: JsonData) {
        this.vendorUid = jsonData.optString(TO_UID)
        this.senderUid = jsonData.optString(FROM_UID)
        this.type = jsonData.optString(TYPE)
        this.attrs = jsonData.optJson(ATTRS)
    }

    fun getType(): String? {
        return type
    }

    fun setType(type: String?): CallMessage {
        this.type = type
        return this
    }

    fun putAttrs(attrs: JsonData?): CallMessage {
        if (attrs == null) {
            return this
        }
        this.attrs = attrs
        return this
    }

    fun toJson(): JsonData {
        val jsonData = JsonData.newMap()
        jsonData.put(TO_UID, vendorUid)
        jsonData.put(FROM_UID, senderUid)
        jsonData.put(TYPE, type)
        jsonData.put(ATTRS, attrs)
        return jsonData
    }

    override fun toString(): String {
        return "CallMessage{" +
                "vendorUid='" + vendorUid + '\''.toString() +
                ", senderUid='" + senderUid + '\''.toString() +
                ", type='" + type + '\''.toString() +
                ", attrs=" + attrs +
                '}'
    }

    companion object {
        private val TO_UID = "toUid"
        private val FROM_UID = "fromUid"
        private val TYPE = "type"
        private val ATTRS = "attrs"

        fun from(vendorUid: String, senderUid: String): CallMessage {
            return CallMessage(vendorUid, senderUid)
        }

        fun from(jsonData: JsonData): CallMessage {
            return CallMessage(jsonData)
        }
    }
}
